package com.yycx.module.bus.provider.strategy.impl;

import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.groovy.provider.script.MyGroovyScriptEngine;
import com.yycx.module.bus.client.entity.hy.BusPolicyPosition;
import com.yycx.module.bus.client.entity.hy.BusPolicySaleman;
import com.yycx.module.bus.client.entity.BusSaleman;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.bus.client.entity.hy.HyOrderDetail;
import com.yycx.module.bus.provider.service.hy.BusPolicyPositionService;
import com.yycx.module.bus.provider.service.hy.BusPolicySalemanService;
import com.yycx.module.bus.provider.service.BusSalemanService;
import com.yycx.module.bus.provider.strategy.SettleStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * 片区服务商结算规则
 *
 * @author zyf
 */
@Service("settleStrategyC")
public class SettleStrategyC implements SettleStrategy {

    @Resource
    private BusSalemanService saleManService;

    @Resource
    private BusPolicyPositionService policyPositionService;

    @Resource
    private BusPolicySalemanService policySalemanService;
    @Autowired
    private MyGroovyScriptEngine groovyScriptEngine;


    @Override
    public void setSettlePolicy(HyOrderDetail orderDetail, HyOrder hyOrder, Long saleManId) {
        //自定义结算政策
        Integer customPolicy = FlymeUtils.getInteger(orderDetail.getCustomPolicy(), 0);
        //查询结算对象
        BusSaleman saleManC = saleManService.getById(saleManId);
        //职务ID
        Long positionId = saleManC.getPositionId();
        //发货日期
        Date sendDate = orderDetail.getSendDate();
        //结算政策ID
        Long policyDrugId = orderDetail.getPolicyDrugId();
        BigDecimal settlePrice = orderDetail.getSettlePriceC();
        BigDecimal settleRate = orderDetail.getSettleRateC();
        //结算公式
        String settleFormulaContent = FlymeUtils.getString(orderDetail.getSettleFormulaContentC(), "(销售单价-片区服务商结算价)*数量*(1-片区服务商标准)-渠道商结算金额;");
        //部门利润公式
        String deptProfitAmountFormula = "(片区服务商结算价-部门结算价)*数量*(1-部门标准);";
        //税率差公式
        String taxRateDiffFormula = "(销售单价-片区服务商结算价)*数量*((1-部门标准)-(1-片区服务商标准));";
        Integer settleRule = 1;
        Integer baopiao = 1;
        Integer hasTaxRateDiff=FlymeUtils.getInteger(hyOrder.getHasTaxRateDiff(),1);
        if (customPolicy.equals(0)) {
            //获取职务结算政策
            BusPolicyPosition busPolicyPosition = policyPositionService.findBusPolicyPosition(policyDrugId, positionId, sendDate);
            if (FlymeUtils.isNotEmpty(busPolicyPosition)) {
                settlePrice = FlymeUtils.getBigDecimal(busPolicyPosition.getPositionPrice(), settlePrice);
                settleRate = FlymeUtils.getBigDecimal(busPolicyPosition.getPositionRate(), settleRate);
                settleFormulaContent = FlymeUtils.getString(busPolicyPosition.getPositionSettleFormula(), settleFormulaContent);
            }
            //获取业务员结算政策
            BusPolicySaleman policySaleMan = policySalemanService.findByPolicyDrugIdAndSaleManId(policyDrugId, saleManId, sendDate);
            if (FlymeUtils.isNotEmpty(policySaleMan)) {
                settlePrice = FlymeUtils.getBigDecimal(policySaleMan.getSaleManPrice(), settlePrice);
                settleRate = FlymeUtils.getBigDecimal(policySaleMan.getSaleManRate(), settleRate);
                settleFormulaContent = FlymeUtils.getString(policySaleMan.getSaleManSettleFormula(), settleFormulaContent);
                //价格取值方式
                Integer priceType = FlymeUtils.getInteger(policySaleMan.getPriceType(), 1);
                if (priceType.equals(2)) {
                    //使用成本价
                    settlePrice = FlymeUtils.getBigDecimal(orderDetail.getCostPrice(), settlePrice);
                }
                //结算规则(回款后结算)
                settleRule = FlymeUtils.getInteger(policySaleMan.getSettleRule(), 1);
                //是否包票(默认包票)
                baopiao = FlymeUtils.getInteger(policySaleMan.getBaopiao(), 1);
            }
        }
        orderDetail.setBaopiao(baopiao);
        orderDetail.setSettleRule(settleRule);
        orderDetail.setSettlePriceC(settlePrice);
        orderDetail.setSettleRateC(settleRate);
        if (customPolicy.equals(1)) {
            settleFormulaContent = orderDetail.getSettleFormulaContentC();
            deptProfitAmountFormula = orderDetail.getDeptProfitAmountFormula();
            taxRateDiffFormula = orderDetail.getTaxRateDiffFormula();
        }
        orderDetail.setSettleFormulaContentC(settleFormulaContent);
        orderDetail.setSaleManIdC(saleManC.getSaleManId());
        //部门利润公式
        orderDetail.setDeptProfitAmountFormula(deptProfitAmountFormula);
        if (FlymeUtils.gtzero(settleRate)) {
            if(hasTaxRateDiff.equals(1)) {
                //税率差公式
                orderDetail.setTaxRateDiffFormula(taxRateDiffFormula);
            }else{
                orderDetail.setTaxRateDiffFormula(null);
            }
        } else {
            //当片区服务商结算标准为0时调整部门利润公式
            orderDetail.setDeptProfitAmountFormula("部门费用-渠道商结算金额-片区服务商结算金额-超标准差额;");
        }
    }

    @Override
    public void setSettleAmount(HyOrderDetail hyOrderDetail, HyOrder hyOrder, Map<String, Object> variable) {
        //片区服务商结算价
        BigDecimal settlePriceC = FlymeUtils.getBigDecimal(hyOrderDetail.getSettlePriceC(), "0");
        if (FlymeUtils.gtzero(settlePriceC)) {
            //片区服务商结算标准
            BigDecimal settleRateC = FlymeUtils.getBigDecimal(hyOrderDetail.getSettleRateC(), "0");
            //销售数量
            BigDecimal saleNum = hyOrderDetail.getSaleNum();
            //销售价
            BigDecimal salePrice = hyOrderDetail.getSalePrice();
            //片区服务商结算金额
            BigDecimal settleAmountC = (salePrice.subtract(settlePriceC)).multiply(saleNum).multiply(settleRateC);
            //片区服务商结算公式
            String settleFormulaC = hyOrderDetail.getSettleFormulaContentC();
            //使用公式重新计算片区服务商结算公式
            if (FlymeUtils.isNotEmpty(settleFormulaC)) {
                settleAmountC = groovyScriptEngine.executeBigDecimal(settleFormulaC, variable,2);
            }
            variable.put("片区服务商结算金额", settleAmountC);
            hyOrderDetail.setSettleAmountC(settleAmountC);
        }
    }
}
